#include "Turret.h"
#define BULLET_VECTOR_0 5
#define PI 3.14159265
#define RANGE_RENDER 6

int CTurret::_GunXPosition[12] = {-32, -32, -16, 0, 16, 32, 32, 32, 16, 0, -16, -32};
int CTurret::_GunYPosition[12] = {0, 16, 32, 32, 32, 16, 0, -16, -32, -32, -32, -16};
double CTurret::_ShootAngle[12] = {180, 150, 120, 90, 60, 30, 0, -30, -60, -90, -120, -150};

CTurret::CTurret(int X, int Y)
{
	_Sprite = CSpriteManager::getInstance()->GetEnemySprite(Enemy::Turret);
	_x = X;
	_y = Y;
	frame_start = GetTickCount();
	tick_per_frame = 1000;
	_CurrentFrame = 0;
	Flag = true;
	_IsShootable = false;
	_ListBullet = new CListBullet(4);
}

CTurret::~CTurret(void)
{
}


void CTurret::Update(D3DXVECTOR2 ContraPosition)
{
	int angle = GetAngle(ContraPosition, this->GetPosition());
	int DestinationFrame;
	_IsShootable = false;
	if(Flag)
	{
		if (abs(ContraPosition.x - _x) > SCREEN_WIDTH / 2 - FRAMEWIDTH)
		{
			_CurrentFrame = 13;
			if (ContraPosition.x - _x > SCREEN_WIDTH / 2 - FRAMEWIDTH)
				Flag = false;
			return;
		}
		else
		{
			DWORD now = GetTickCount();
			if (now - frame_start >= tick_per_frame)
			{
				frame_start = now;
				if(angle >= 15 && angle < 45)
				{
					DestinationFrame = 5;	
				}
				if(angle >= 45 && angle < 75)
				{
					DestinationFrame = 4;	
				}
				if(angle >= 75 && angle < 105)
				{
					DestinationFrame = 3;	
				}
				if(angle >= 105 && angle < 135)
				{
					DestinationFrame = 2;	
				}
				if(angle >= 135 && angle < 165)
				{
					DestinationFrame = 1;
				}
				if(angle >= 165 && angle < 195)
				{
					DestinationFrame = 0;
				}
				if(angle >= 195 && angle < 225)
				{
					DestinationFrame = 11;
				}
				if(angle >= 225 && angle < 255)
				{
					DestinationFrame = 10;
				}
				if(angle >= 255 && angle < 285)
				{
					DestinationFrame = 9;
				}
				if(angle >= 285 && angle < 315)
				{
					DestinationFrame = 8;
				}
				if(angle >= 315 && angle < 345)
				{
					DestinationFrame = 7;
				}
				if(angle >= 345 ||angle < 15)
				{
					DestinationFrame = 6;
				}
				if(_CurrentFrame != DestinationFrame)
				{
					if(DestinationFrame > _CurrentFrame)
					{
						if(abs(DestinationFrame - _CurrentFrame) > 5)
						{
							_CurrentFrame = Previous(_CurrentFrame,0,11);
						}
						else
						{
							_CurrentFrame = Next(_CurrentFrame,0,11);
						}
					}
					else
					{
						if(_CurrentFrame - DestinationFrame > 5)
						{
							_CurrentFrame = Next(_CurrentFrame,0,11);
						}
						else
						{
							_CurrentFrame = Previous(_CurrentFrame,0,11);
						}
					}
				}
				else
				{
					_IsShootable = true;
				}
			}
		}
	}
}

void CTurret::Render(D3DXVECTOR2 ContraPosition)
{

	if ((ContraPosition.x < SCREEN_WIDTH / 2 && abs(ContraPosition.x - _x) <= SCREEN_WIDTH + FRAMEWIDTH) || abs(ContraPosition.x - _x) < SCREEN_WIDTH / 2 + FRAMEWIDTH || (ContraPosition.x > GAMEWORLDWIDTH - SCREEN_WIDTH / 2 && abs(_x - ContraPosition.x) < SCREEN_WIDTH + FRAMEWIDTH))
	{
		DWORD now = GetTickCount();
		Update(ContraPosition);

		if(_IsShootable)
		{
			_ListBullet->Shoot(_x + _GunXPosition[_CurrentFrame], _y + _GunYPosition[_CurrentFrame], 3000, BULLET_VECTOR_0 * cos(_ShootAngle[_CurrentFrame]/180*PI), BULLET_VECTOR_0 * sin(_ShootAngle[_CurrentFrame]/180*PI), 2);
		}
		_ListBullet->Render();

		_Sprite->Render(_x, _y, _CurrentFrame);
	}
}

Box CTurret::GetBox()
{
	return Box();
}